package com.tb.service;

import com.tb.mapper.QCommentMapper;
import com.tb.model.QComment;
import com.tb.model.QCommentExample;
import com.tb.util.MyConstant;
import com.tb.util.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: wangguichun
 * @create: 2018-04-14 21:46
 **/

@Service
public class QCommentService {

    @Autowired
    private QCommentMapper qCommentMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private SensitiveWordService sensitiveWordService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private IllegalityService illegalityService;

    public List<QComment> getCommentByQuestionId(Integer questionId) {
        QCommentExample example = new QCommentExample();
        QCommentExample.Criteria criteria = example.createCriteria();
        criteria.andQuestionIdEqualTo(questionId);
        List<QComment> qComments = qCommentMapper.selectByExample(example);
        //注入评论的用户信息
        for (QComment qComment : qComments) {
            qComment.setUser(userService.getUserById(qComment.getUserId()));
        }
        return qComments;
    }

    public void addComment(QComment qComment) {
        String content = qComment.getContent();
        qComment.setContent(MyUtils.purifyString(content,sensitiveWordService.getStringList()));
        qCommentMapper.insertSelective(qComment);
    }

    public QComment getQuestionCommentById(Integer objectId) {
        return qCommentMapper.selectByPrimaryKey(objectId);
    }

    public void deleteQuestionCommentById(int id) {
        //删除之前连带删除消息表和违规记录表中的记录
        messageService.removeRecordByTypeAndObjectId(MyConstant.ACT_COMMENT_QUES,id);
        illegalityService.removeRecordByTypeAndObjectId(MyConstant.ACT_COMMENT_QUES,id);
        //真正删除问题记录
        qCommentMapper.deleteByPrimaryKey(id);
    }

}
